﻿using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class yingxiaoDAL
    {
        public yingxiaoDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="biaoti">标题</param>
        /// <param name="count"></param>
        /// <param name="sum"></param>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <returns></returns>
        public List<yingxiao> Showying(string? biaoti,out int count,out int sum,int index=1,int size=2)
        {
            try
            {
                var list=Context.yingxiaos.ToList();
                if(string.IsNullOrEmpty(biaoti) )
                {
                    list=list.Where(x=>x.biaoti.Contains(biaoti)).ToList();
                }
                count=list.Count();
                sum = (int)Math.Ceiling(count * 1.0 / size);
                list=list.OrderBy(x=>x.Id).Skip((index-1)*size).Take(size).ToList();
                return list;

            }
            catch (Exception ex)
            {

                throw ;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">返回受影响的行数</param>
        /// <returns></returns>
        public int Delying(int id)
        {
            try
            {
                var del=Context.yingxiaos.FirstOrDefault(x => x.Id == id);
                if(del != null)
                {
                    Context.yingxiaos.Remove(del);
                }
                return Context.SaveChanges();
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="yingxiao"></param>
        /// <returns></returns>
        public int Upying(yingxiao yingxiao)
        {
            var up = Context.yingxiaos.Find();
            if(up != null)
            {
                up.leixing=yingxiao.leixing;
                up.biaoti=yingxiao.biaoti;
                up.fubiaoti=yingxiao .fubiaoti;
                up.tupian=yingxiao .tupian;
                up.kucun=yingxiao .kucun;
                up.xianling=yingxiao .xianling;
                up.tiaojian=yingxiao .tiaojian;
                up.jiner=yingxiao .jiner;
                up.shijian=yingxiao .shijian;
                up.xuzhi=yingxiao .xuzhi;
                up.mendian=yingxiao .mendian;
                up.phone=yingxiao .phone;
            }
            return Context.SaveChanges();
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <returns></returns>
        public yingxiao fantian()
        {
            try
            {
                return Context.yingxiaos.Find();
            }
            catch (Exception)
            {

                throw;
            }
        }









    }
}
